package com.oa.co.controller;

import com.alibaba.excel.event.Order;
import com.github.pagehelper.PageInfo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.pojo.OrderRoom;
import com.oa.co.pojo.Room;
import com.oa.co.service.OrderRoomService;
import com.oa.co.service.RoomService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @Author Lone Ranger
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/meeting")
public class OrderRoomController {

    @Autowired
    private OrderRoomService orderRoomService;


    // 预约会议
    @RequestMapping("/addorder")
//    public R addOrder(String orderid, Date ordertime,String username,String department,String meettheme,Date starttime,Date endtime,Integer roomid,Integer peoplenum){
    public R addOrder(@RequestBody OrderRoom orderRoom) {
        System.out.println(orderRoom);
        Integer num = orderRoomService.addOrder(orderRoom);
        if (num == 1) {
            return R.ok().message("预约成功");
        }
        return R.error().message("预约失败");
    }

    // 修改预约会议
    @RequestMapping("/editOrder")
//    public R addOrder(String orderid, Date ordertime,String username,String department,String meettheme,Date starttime,Date endtime,Integer roomid,Integer peoplenum){
    public R editOrder(@RequestBody OrderRoom orderRoom) {
        System.out.println(orderRoom);
        Integer num = orderRoomService.modifyOrder(orderRoom);
        if (num == 1) {
            return R.ok().message("预约已修改");
        }
        return R.error().message("预约修改失败");
    }

    // 查询所有预约信息
    @RequestMapping("/allorder")
    public PageVo<OrderRoom> orderList(Integer page,Integer limit,String username, String department){

        PageVo<OrderRoom> pageVo = new PageVo<>();
        //使用 mybatis的分页插件
        PageInfo<OrderRoom> pageData = orderRoomService.getAllOrders(page, limit, username, department);

        // 把分页查询结果 封装到 layui 的 列表组件支持的数据结构中
        pageVo.setData(pageData.getList());

        pageVo.setCode(0);

        pageVo.setCount(pageData.getTotal());

        pageVo.setMsg("预约会议室的信息");
        return pageVo;
    }

    // 删除一个预约信息
    @RequestMapping("/deleteorder")
    public R deleteOrder(String order_id){
//        System.out.println(order_id);
        Integer num = orderRoomService.deleteOrder(order_id);
        if (num == 1) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");

    }

    // 删除多个预约信息
    @RequestMapping("/deleteOrderMulti")
    public R deleteOrderMulti(@RequestBody List<OrderRoom> list){
//        System.out.println(list);
        Integer num = orderRoomService.deleteOrderMulti(list);
        if (num == list.size()) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");

    }

}
